/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2008 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#ifndef INCL_FUN_CONSTANTS
#define INCL_FUN_CONSTANTS


#define UNUSED_LONG_VALUE           2592000     // 60*60*24*30 = sec in a month of 30 days
#define MAX_RRULE_RECORD_NUMBER     6           // the maximum number of record of rrule that we want fill for no end date event
#define MAX_OCCURRENCES_ALLOWED     999         // the maximum number of occurrences allowed for recurring event 

#define EMPTY_DATE                  "19700101T000000"   // the default empty date unused

#define TABLE_EVENT                     "EVENT"
#define TABLE_RECURRENCE                "RECURRENCE"
#define QUERY_MAX_ID_FROM_EVENT         "SELECT MAX(ROWID) from Event;"
#define QUERY_MAX_ID_FROM_RECURRENCE    "SELECT MAX(ROWID) from Recurrence;"
#define QUERY_MAX_ID_FROM_CALENDAR      "SELECT MAX(ROWID) from Calendar;"
#define TABLE_CALENDAR_CHANGES          "CALENDARCHANGES"
#define TABLE_OCCURRENCE_CACHE          "OCCURRENCECACHE"
#define TABLE_EVENT_CHANGES             "EVENTCHANGES"
#define TABLE_RECURRENCE_CHANGES        "RECURRENCECHANGES"

#define QUERY_ROW_ID_RECURRENCE             "select ROWID from Recurrence where event_id = "
#define QUERY_CALENDAR_ID_FROM_RECURRENCE   "select distinct calendar_id from recurrencechanges where event_id_tomb = "

#define MAX_NUMBER              40              // the max number of occurrences that we want to store in the occurrenceCache
#define MAX_NUMBER_OCCUR        MAX_NUMBER + 1  // it is incremeted by one due to the alghoritm to fill the occurrences. Used
                                                // to calculate the end day of the recurrence



#define TABLE_ALARM                     "ALARM"
#define TABLE_ALARM_CHANGES             "ALARMCHANGES"
#define QUERY_MAX_ID_FROM_ALARM         "SELECT MAX(ROWID) from Alarm;"
#define QUERY_GET_ALARM_ID              "SELECT ROWID from Alarm where entity_id = "

#define SELECT_EVENT_ID                 "SELECT event_id from fun_event_state where"
#define SELECT_ALL_EVENT_ID             "SELECT ROWID from Event;"

#define CREATE_FUN_EVENT_STATE  "CREATE TABLE IF NOT EXISTS fun_event_state (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, event_id INTEGER, \
                                 creation INTEGER, updated INTEGER, deleted INTEGER DEFAULT 0, updated_from_client INTEGER DEFAULT 0, \
                                 sent_status INTEGER DEFAULT 0);"

#define TRIGGER_EVENT_NEW      "CREATE TRIGGER change_fun_event_state_on_insert AFTER INSERT ON Event \
                                BEGIN \
                                INSERT INTO fun_event_state(event_id, creation, updated, deleted, updated_from_client, sent_status) VALUES \
                                (NEW.ROWID, strftime('%s', 'now'), 0, 0, 0, 0); \
                                END;"

#define TRIGGER_EVENT_UPDATE  "CREATE TRIGGER change_fun_event_state_on_update AFTER UPDATE ON Event \
                                BEGIN \
                                UPDATE fun_event_state SET updated = strftime('%s', 'now') WHERE event_id = OLD.ROWID; \
                                END;"

#define TRIGGER_EVENT_DELETE  "CREATE TRIGGER change_fun_event_state_on_delete AFTER DELETE ON Event \
                                BEGIN \
                                UPDATE fun_event_state SET deleted = strftime('%s', 'now') WHERE event_id = OLD.ROWID; \
                                END;"

/* 
 * Triggers for the Alarm modifications events. 
 * Only update needs
 */

#define TRIGGER_ALARM_UPDATE    "CREATE TRIGGER change_fun_event_state_on_update_alarm AFTER UPDATE ON Alarm \
                                BEGIN \
                                UPDATE fun_event_state SET updated = strftime('%s', 'now') WHERE event_id = OLD.entity_id; \
                                END;"

#define TRIGGER_ALARM_DELETE    "CREATE TRIGGER change_fun_event_state_on_delete_alarm AFTER DELETE ON Alarm \
                                BEGIN \
                                UPDATE fun_event_state SET updated = strftime('%s', 'now') WHERE event_id = OLD.entity_id; \
                                END;"


/*** Triggers for the Recurrence events modifications. ****/
/** deletion and insert are already detected */

#define TRIGGER_RECURRENCE_UPDATE   "CREATE TRIGGER change_fun_event_state_on_update_recurrence AFTER UPDATE ON Recurrence \
                                    BEGIN \
                                    UPDATE fun_event_state SET updated = strftime('%s', 'now') WHERE event_id = OLD.event_id; \
                                    END;"

#define EVENT_ALL_ROW  "select ROWID from Event;"   

#define CALENDAR_NUM_ROW "select count(*) from Calendar;"

#define INITIALIZE_CALENDAR_TABLE "insert into Calendar (store_id, title, read_only, hidden, color_r, color_g, color_b, \
                                    color_is_display, type, external_id, external_mod_tag, external_id_tag, external_rep) values \
                                    (1, 'Calendar', 0, 0, -1, -1, -1, \
                                    0, NULL, NULL, NULL, NULL, NULL);"

#define INITIALIZE_CALENDAR_CHANGES_TABLE1 "insert into CalendarChanges (record, type, store_id, external_id) values \
                                            (2, 0, 1, NULL);"
#define INITIALIZE_CALENDAR_CHANGES_TABLE2 "insert into CalendarChanges (record, type, store_id, external_id) values \
                                            (2, 1, 1, NULL);"




#endif

